package com.cg;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) throws IOException, InterruptedException {
//        test01();
//        test02();
        String URL = "https://image.baidu.com/search/albumsdetail?tn=albumsdetail&word=城市建筑摄影专题&fr=searchindex_album%20&album_tab=建筑&album_id=7&rn=30";
        Document javaDocument = Jsoup.parse(new URL(URL), 10000);

        Document document = Jsoup.connect(URL).get();
        System.out.println(document);
        Elements elements = document.getElementsByClass("albumsdetail-column");

        elements.forEach(item -> {
            Elements select = item.select("a[href]");
            System.out.println(select.attr("abs:href"));
        });

    }

    private static void test02() throws IOException {
        // 使用jsoup解析url
        Document doc = Jsoup.parse(new URL("https://www.autohome.com.cn/bestauto/"), 10000);

        // 把获取的内容输出为文件
        FileUtils.writeStringToFile(new File("D:/test.txt"), doc.toString(), "UTF-8");

        // 获取title
        Element element = doc.getElementsByTag("video").first();

// 打印title内容
        System.out.println(element.text());
    }

    private static void test01() throws IOException {
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 声明访问地址
        HttpGet httpGet = new HttpGet("https://www.autohome.com.cn/bestauto/");

        // 发起请求
        CloseableHttpResponse response = httpClient.execute(httpGet);

        // 判断状态码是否是200
        if (response.getStatusLine().getStatusCode() == 200) {
            // 解析数据
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.out.println(content);
        }
    }
}